package com.handinglian.ai.notice.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.apidoc.annotation.Api;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.handinglian.ai.common.dto.CommonPage;
import com.handinglian.ai.common.dto.ResultData;
import com.handinglian.ai.common.util.FastJsonUtil;
import com.handinglian.ai.notice.dto.*;
import com.handinglian.ai.notice.param.NoticeCreateParam;
import com.handinglian.ai.notice.param.NoticeUpdateParam;
import com.handinglian.ai.notice.service.MassService;
import com.handinglian.ai.notice.service.NoticeService;
import com.handinglian.ai.record.enums.PhoneStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Api("普通通知")
@RestController
@Slf4j
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private MassService massService;
    
    /**
     * 创建通知
     */
    @PostMapping(value = "/create")
    public ResultData create(@RequestBody NoticeCreateParam createParam) throws Exception {
        return ResultData.success(noticeService.create(createParam));
    }

    /**
     * 删除通知
     */
    @DeleteMapping(value = "/delete")
    public ResultData delete(Integer id) {
        return ResultData.success(noticeService.delete(id));
    }

    /**
     * 批量删除通知
     */
    @DeleteMapping(value = "/deleteBatch")
    public ResultData deleteBatch(String id) {
        return ResultData.success(noticeService.deleteBatch(id));
    }

    /**
     * 更新通知
     */
    @PutMapping(value = "/update")
    public ResultData update(@RequestBody NoticeUpdateParam updateParam) {
        return ResultData.success(noticeService.update(updateParam));
    }

    /**
     * 获取通知列表
     */
    @GetMapping(value = "/listPage")
    public ResultData<CommonPage<NoticeDto>> listPage(String titleOrTemplate,
                                                      String type,
                                                      String category,
                                                      String method,
                                                      String executeState,
                                                      String sortField,
                                                      String sortOrder,
                                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return ResultData.success(noticeService.listPage(titleOrTemplate, type, category, method, executeState, sortField, sortOrder, pageNum, pageSize));
    }

    /**
     * 获取通知基本信息
     */
    @GetMapping(value = "/get")
    public ResultData<NoticeBaseDto> get(Integer id) {
        return ResultData.success(noticeService.getNoticeBase(id));
    }

    /**
     * 获取更新的基本信息
     */
    @GetMapping(value = "/getSimple")
    public ResultData<NoticeSimpleDto> getSimple(Integer id) {
        return ResultData.success(noticeService.getSimple(id));
    }

    /**
     * 获取通知详情列表
     */
    @GetMapping(value = "/listDetail")
    public ResultData<CommonPage<NoticeDetailDto>> listDetail(Integer id, String nameOrPhoneOrAddress, String keyOrResult,
                                                  String startTime, String endTime, String phoneStatus, String sortField,
                                                  String sortOrder,
                                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return ResultData.success(noticeService.listDetail(id, nameOrPhoneOrAddress, keyOrResult, startTime, endTime, phoneStatus, sortField, sortOrder, pageNum, pageSize));
    }

    /**
     * 删除通知详情
     */
    @GetMapping(value = "/deleteDetail")
    public ResultData deleteDetail(Integer massId) {
        return ResultData.success(noticeService.deleteDetail(massId));
    }

    /**
     * 批量删除通知详情
     */
    @GetMapping(value = "/deleteDetailBatch")
    public ResultData deleteDetailBatch(List<Integer> massIds) {
        return ResultData.success(noticeService.deleteDetailBatch(massIds));
    }

    /**
     * 通知详情导出
     */
    @GetMapping("/exportNoticeDetail")
    public void exportNoticeDetail(Integer id, String nameOrPhoneOrAddress, String keyOrResult,
                                   String startTime, String endTime, String phoneStatus, String sortField,
                                   String sortOrder,
                                   ModelMap map, HttpServletRequest request, HttpServletResponse response) {

        CommonPage<NoticeDetailDto> commonPage = noticeService.listDetail(id, nameOrPhoneOrAddress, keyOrResult, startTime, endTime, phoneStatus, sortField, sortOrder, 1, 999);
        List<NoticeDetailExcel> noticeDetailExcels = FastJsonUtil.ListToList(commonPage.getList(), NoticeDetailExcel.class);
        noticeDetailExcels.forEach(noticeDetailExcel -> {
            String description = PhoneStatusEnum.valueOfKey(Integer.valueOf(noticeDetailExcel.getPhoneStatus())).getDescription();
            noticeDetailExcel.setPhoneStatus(description);
        });
        ExportParams params = new ExportParams("通知详情", "通知详情", ExcelType.XSSF);
        map.put(NormalExcelConstants.DATA_LIST, noticeDetailExcels);
        map.put(NormalExcelConstants.CLASS, NoticeDetailExcel.class);
        map.put(NormalExcelConstants.PARAMS, params);
        map.put(NormalExcelConstants.FILE_NAME, "通知详情");
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }

    /**
     * 人口普查模板导出
     */
    @GetMapping("/exportCensusExcel")
    public void exportCensusExcel(ModelMap map, HttpServletRequest request, HttpServletResponse response) {
        ExportParams params = new ExportParams("通知人员列表", "通知人员列表", ExcelType.XSSF);
        map.put(NormalExcelConstants.DATA_LIST, new ArrayList<>());
        map.put(NormalExcelConstants.CLASS, CensusExcel.class);
        map.put(NormalExcelConstants.PARAMS, params);
        map.put(NormalExcelConstants.FILE_NAME, "通知人员列表");
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }

    /**
     * 人口普查信息导入
     */
    @PostMapping("/importCensusExcel")
    public ResultData<ImportCensusDto> importCensusExcel(ModelMap map, HttpServletRequest request, HttpServletResponse response, MultipartFile file, Integer noticeId, String noticeTime, Integer staff) throws Exception {
        ImportCensusDto importCensusDto = massService.importCensusExcel(map, request, response, file, noticeId, noticeTime, staff);
        return ResultData.success(importCensusDto);
    }

}
